// 资源详情js
var $scope = null;
$(function () {
	var appElement = document.querySelector('[ng-controller=main]');
	$scope = angular.element(appElement).scope();
	var imageTypeId = locationHref("imageTypeId");
	$scope.imageTypeId = imageTypeId;
	var pageNumber = locationHref("pageNumber");
	if (pageNumber && pageNumber == 1) {
		initPaginator(1);
		$scope.methods.imageList(1);
	} else if (pageNumber && pageNumber != 1) {
		initPaginator(pageNumber);
		$scope.methods.imageList(pageNumber);
	} else {
		initPaginator(1);
		$scope.methods.imageList(1);
	}
	initialPage($scope);
	//  initPaginator(1);
	// $scope.methods.imageList(1);

});
// 初始化轮播
function initialPage($scope) {

}
function initPaginator(page) {
	var optionsInit = {
		currentPage: 1,//当前的请求页数。
		totalPages: 1,//一共多少页。
		size: "normal",//应该是页眉的大小。
		bootstrapMajorVersion: 3,//bootstrap的版本要求。
		alignment: "right",
		numberOfPages: 15,//一页列出多少数据。
	};
	$("#pageLimit").bootstrapPaginator(optionsInit);
}
var mainApp = angular.module("mainApp", []);
mainApp.controller('main', function ($scope) {
	$scope.currentImageIndex = 0;
	$scope.dynamicData = [];
	$scope.search = {};
	$scope.methods = {
		//动态列表
		imageList: function (page) {
			var params = '';
			params += '&pageNum=' + page
			params += '&pageSize=15'
			params += '&imageTypeId=' + $scope.imageTypeId
			params += '&isPublic=Y'
			params += '&status=1'
			$.ajax({
				url: $.shysUrlPrefix + '/website/imageList?' + params,
				type: 'get',
				dataType: 'JSON',
				async: true, //同步方式发起请求
				success: function (data) {
					if (data.rows != null && data.rows.length > 0) {
						$.each(data.rows, function (i, item) {
							if (data.rows[i].imageUrl) {
								data.rows[i].imageUrl = data.rows[i].imageUrl + '?x-oss-process=image/watermark,color_FFFFFF,text_QOahpeaigeWcqOe6vw/resize,m_fill,h_165,w_240'
							}
						}),
							// ?x-oss-process=image/resize,1_923,h_600/format,heic
							$scope.dynamicData = data.rows;
						$scope.imageTypeName = data.rows[0].imageTypeName; //
						$scope.createTime = data.rows[0].createTime; //
						console.log(data.rows)
						$scope.totalNumber = data.total;
						$scope.$apply();
						var totalPages = Math.ceil(data.total / 15);
						if (totalPages < 2) {
							totalPages = 1;
						}
						var options = {
							currentPage: page,//当前的请求页数。
							totalPages: totalPages,//一共多少页。
							size: "normal",//应该是页眉的大小。
							bootstrapMajorVersion: 5,//bootstrap的版本要求。
							alignment: "right",
							numberOfPages: totalPages > 25 ? 25 : totalPages,//显示按钮的数量。
							shouldShowPage: true,
							itemTexts: function (type, page, current) {//如下的代码是将页眉显示的中文显示我们自定义的中文。
								switch (type) {
									case "first": return "首页";
									case "prev": return "<";
									case "next": return ">";
									case "last": return "尾页";
									case "page": return page;
								}
							},
							onPageClicked: function (event, originalEvent, type, page) {//给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
								//保存页数
								// $scope.pageNumber=page;
								localStorage.setItem('pageNumber', page);
								console.log(page)
								$scope.methods.imageList(page);

							}
						};
						$("#pageLimit").bootstrapPaginator(options);
					} else {

					}

				},

			})
		},
		// 第三级
		labelDetailClick: function (label) {
			$scope.pageNumber = localStorage.getItem('pageNumber');
			if (!$scope.pageNumber) {
				$scope.pageNumber = 1;
			}
			if (label.imageTypeId) {
				window.location.href = "resource-Detail.html?imageTypeId=" + label.imageTypeId + "&imageId=" + label.imageId + "&pageNumber=" + $scope.pageNumber
			}

		},
		// 图集跳转
		//影音跳转
		audio: function () {
			window.location.href = "resource.html?dynamicTypeId=1"
		},
	};
});
